package com.sc.web.goods;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sc.dal.model.goods.GoodsStock;
import com.sc.service.goods.GoodsStockService;
import com.sc.util.data.DataUtil;
import com.sc.util.exception.ExceptionEnum;
import com.sc.util.exception.ScBizException;

/**
 * 商品库存管理
 * @author maoyongbin
 * @日期 Mar 30, 2017 9:50:56 AM
 */
@Controller
@RequestMapping(value="goodsStock")
public class GoodsStockController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private GoodsStockService goodsStockService;
	
	/**
	 * 查询商品库存数据
	 * @param goodsId
	 * @return Map<String, Object>
	 */
	@RequestMapping(value="selectList", method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> selectList(String goodsId,String flag){
		Map<String,Object> resultMap = new LinkedHashMap<String,Object>();
		
		if(StringUtils.isBlank(goodsId)){
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_PARAM);
			logger.warn("查询参数有误!");
		}else{
			try{
				//查询商品数据
				Map<String, Object> paramMap = new HashMap<String, Object>();
				paramMap.put("goodsId", goodsId);
				
				List<GoodsStock> goodsStockList = this.goodsStockService.selectList(paramMap);
				//处理数据，以便在回显商品库存（货品）表使用
				for(GoodsStock goodsStock : goodsStockList){
					StringBuilder skuValueSb = new StringBuilder();
					//获取规格相关的数据
					if(StringUtils.isNotBlank(goodsStock.getFreeItem0())){
						skuValueSb.append(goodsStock.getFreeItem0()).append("、");
					}
					if(StringUtils.isNotBlank(goodsStock.getFreeItem1())){
						skuValueSb.append(goodsStock.getFreeItem1()).append("、");
					}
					if(StringUtils.isNotBlank(goodsStock.getFreeItem2())){
						skuValueSb.append(goodsStock.getFreeItem2()).append("、");
					}
					if(StringUtils.isNotBlank(goodsStock.getFreeItem3())){
						skuValueSb.append(goodsStock.getFreeItem3()).append("、");
					}
					
					if(skuValueSb.length()>0){
						goodsStock.setSkuValue(skuValueSb.toString().substring(0, skuValueSb.length()-1));
					}
				}
				
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS, goodsStockList);
				logger.info("查询商品数据成功!");
			}catch(Exception e){
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
				logger.error("查询商品数据异常:"+e);
				throw new ScBizException("查询商品数据异常！", e);
			}
		}
		
		return resultMap;
	}

}
